//
//  UserAccount.swift
//  Weibo
//
//  Created by gdarkness on 16/3/20.
//  Copyright © 2016年 gdarkness. All rights reserved.
//

import UIKit

class UserAccount: NSObject,NSCoding {

        //用于条用access——token,接口获取授权后的access token
    var access_token: String?
    //access_token的生命周期，单位是秒数
    //在swift中如果是基本数据类型定义为可选，在KVC回找不到这个key
    var expires_in:NSTimeInterval = 0{
        didSet{
            //当别人给expires_in 属性赋值时，我们将这个expires_in转成expiresData方便比较时间
            expiresDate = NSDate(timeIntervalSinceNow: expires_in)
        }
    }
    //过期时间，用于比较是否过期
    var expiresDate:NSDate?
    //当前授权用户的UID
    var uid:String?
    //用户昵称
    var screen_name:String?
    //用户头像地址（大图）.180X180像素
    var avatar_large:String?
    //字典转模型
    init(dict:[String:AnyObject]){
        super.init()
        //只有super.init（）后对象才创建完毕，对象创建完毕后才调用对象方法
        setValuesForKeysWithDictionary(dict)
    }
    //当字典的key在模型中找不到对应的属性
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    override var description: String{
        //只读计算性属性
        get{
            let keys = ["access_token","expires_in","expiresDate","uid","screen_name","avatar_large"]
            //拿着传入的key去模型中找所对应的属性，找到属性值和key的值将会拼成字典
            return dictionaryWithValuesForKeys(keys).description
        }
    }
    
    
    //MARK: -- 归档和解档
    //归档
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(access_token,forKey: "access_token")
        aCoder.encodeDouble(expires_in, forKey: "expires_in")
        aCoder.encodeObject(expiresDate,forKey: "expiresDate")
        aCoder.encodeObject(uid,forKey: "uid")
        aCoder.encodeObject(screen_name,forKey: "screen_name")
        aCoder.encodeObject(avatar_large,forKey: "avatar_large")
        
    }
    //解档
    required init?(coder aDecoder:NSCoder){
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        expires_in = aDecoder.decodeDoubleForKey("expires_in")
        expiresDate = aDecoder.decodeObjectForKey("expiresDate") as? NSDate
        uid = aDecoder.decodeObjectForKey("uid") as? String
        screen_name = aDecoder.decodeObjectForKey("screen_name") as?String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
        
    }
    //只读计算性属性
//    override var description :String{
//        get{
//            return "access_token: \(asscess_token),expires_in:\(expires_in),uid:\(uid),"
//            let key = ["access_token","expires_in","uid"]
//            //key 是模型中的属性，通过key去模型中回去属性对应的值，并且将key和获取到的值组成一个字典
//            return "UserAccount模型:\(dictionaryWithValueForKeys(key).descrption)"
//        }
//    }
//    override var description: String{
//        //当计算性属性只有get方法时，可以省略
//        let key = ["access_token","expires_in","expiresDate","uid","screen_name","avatar_large"]
//        return "UserAccount模型: \(dictionaryWithValuesForKeys(key).description)"
//    }
    
}
